package com.lczyfz.wisdom.common.utils;

import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;
/**
 * @author JackPan
 * */
public class MapUtils {
    private MapUtils(){}

    public static Map<String,Integer> sortMapDesc(Map<String,Integer> src){
        Map<String, Integer> result = src.entrySet().stream()
                .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
                        (oldValue, newValue) -> oldValue, LinkedHashMap::new));

        return result;
    }

    public static Map<String,Integer> sortMapAsc(Map<String,Integer> src){
        Map<String, Integer> result = src.entrySet().stream()
                .sorted(Map.Entry.comparingByValue(Comparator.naturalOrder()))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
                        (oldValue, newValue) -> oldValue, LinkedHashMap::new));

        return result;
    }
}
